package com.nektos.ccf.client;

import java.util.LinkedList;
import java.util.List;

import com.google.gwt.editor.client.LeafValueEditor;
import com.google.gwt.user.client.ui.CheckBox;

public class CheckBoxList implements LeafValueEditor<List<String>> {
    private final List<CheckBox> checkboxes  = new LinkedList<CheckBox>();

	@Override
	public void setValue(List<String> value) {
        for(CheckBox cb: checkboxes) {
            cb.setValue( value != null && value.contains(cb.getText()) );
        }
	}

	@Override
	public List<String> getValue() {
        List<String> vals = new LinkedList<String>();
        for(CheckBox cb: checkboxes) {
        	if(cb.getValue())
                vals.add(cb.getText());
        }
        return vals;
	}
    
	public CheckBox add(String text,String description) {
        int idx=0;
        if((idx=checkboxes.indexOf(text)) != -1)
            return checkboxes.get(idx);
        
        CheckBox cb = new DescriptiveCheckBox(text,description);
        this.checkboxes.add(cb);
        return cb;
        

	}

}
